package com.example.onlinemonitoring.comCenter;

import com.example.onlinemonitoring.Util.CRC16;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.Util.HexUtil;
import com.example.onlinemonitoring.Util.IEEE754Utils;
import com.example.onlinemonitoring.model.Appdevice;
import com.example.onlinemonitoring.model.Byqpdsensor;
import com.example.onlinemonitoring.model.Dlhlsepsensor;
import com.example.onlinemonitoring.serviceImpl.ByqpdsensorServiceImpl;
import com.example.onlinemonitoring.serviceImpl.DlhlsepsensorServiceImpl;

import org.apache.xmlbeans.impl.store.PathDelegate.SelectPathInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Scope;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service("dlhlsep4gCom")
@Scope("prototype")
public class Dlhlsep4gCom extends CommPortManager {
    @Autowired
    private DlhlsepsensorServiceImpl dlhlsepsensorserviceimpl;
    HashMap<Integer, String> devmap = new HashMap<>();

    @Override
    @Transactional
    public void receiveData(String str)  {
        setText("收到sanway请求：" + str + "\r\n");
        if ("7E".equals(str.substring(0, 2))==false) {
            return;
        }
        if (str.length() == 16||str.length() == 20) {
            devmap.clear();
            int startAddr = 0;
            int addrLength = 8;
            int addr = parseHex4(str.substring(2, 4));
            Dlhlsepsensor dlhl = new Dlhlsepsensor();
            dlhl.setStationid(ad.getStationid());
            dlhl.setDevicegroup(ad.getDevicegroup());
            dlhl.setDeviceaddr(addr);
            //List<Dlhlsepsensor> list = dlhlsepsensorserviceimpl.getLists(dlhl);
            Dlhlsepsensor record = dlhlsepsensorserviceimpl.getModel(dlhl);
            int index = 0;
            //for (int i = 0; i < list.size(); i++) {
            //    dlhl = list.get(i);
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastacurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastbcurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastccurrent()) * 100)));
                devmap.put(index++, String.format("%04x", Math.round(isnull(record.getLastdcurrent()) * 100)));
                //devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLasta1current()) * 100)));
                //devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastb1current()) * 100)));
                //devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastc1current()) * 100)));
                //devmap.put(index++, String.format("%04x", Math.round(isnull(dlhl.getLastd1current()) * 100)));
            //}
            /*--------------- 这一段自定义代码 --------------- */

            String head = "7F" + String.format("%02x", addr);
            StringBuilder sb = new StringBuilder();
            for (int i = startAddr; i < startAddr + addrLength; i++) {
                if (i < (devmap.size() + startAddr)) {
                    sb.append(devmap.get(i));
                } else {
                    sb.append("0000");
                }
            }
            //if(addr==list.size())
            //{
            try {
                
                String s2 =  string2HexStringw(String.format("%02d", record.getLastsignalpower())) ;
                Send(s2);
                setText("向"+ ad.getName() + "设备发送指令" + s2);
                Thread.sleep(200);
            } catch (InterruptedException e) {
                setText(e.toString());
            }                
            //}
            
            String s = head + "8041" +String.format("%04x", sb.length() / 2) + sb.toString() ;
            s += CRC16.calcCrc16(s);
            Send(s);
            setText("向"+ ad.getName() + "设备发送指令" + s);

        }
    }

    // @Async("threadPool")
    // public void startSendThread(Appdevice Appdevice) throws Exception {
    //     checkType = "crc16";
    //     issend.put(Thread.currentThread().getId(), true);
    //     String cstr = "";
    //     String cmdtype = "";
    //     String nametype = "";
    //     int timersp = (int) Math.round(Appdevice.getTimersp() * 1000);
    //     while (issend.get(Thread.currentThread().getId()) != null) {
    //         if (addrindex > Appdevice.getAddrmax() || addrindex < Appdevice.getAddrmin())
    //             addrindex = Appdevice.getAddrmin();
    //             // if(addrid>=ssList.size())
    //             // {
    //             //     addrid=0;
    //             // }
    //             Dlhlsepsensor dlhlsensor = new Dlhlsepsensor();
    //             //设置设备地址
    //             dlhlsensor.setStationid(ad.getStationid());
    //             dlhlsensor.setDevicegroup(ad.getDevicegroup());
    //             dlhlsensor.setDeviceaddr(addrindex);
    //             //查询对象
    //             Dlhlsepsensor record = dlhlsepsensorserviceimpl.getModel(dlhlsensor);
    //             if (record == null) {
    //                 setText("当前数据库无监测点：设备地址" + addrindex);
    //             }
    //         cstr = String.format("%02x", record.getLastsignalpower()) ;
    //         //cstr = cstr + CRC16.calcCrc16(cstr);
    //         try {
    //             if(ssList.size()>0)
    //             {
    //                 for(int i=0;i<ssList.size();i++)
    //                 {
    //                     if(ssList.get(i).isConnected()&&!ssList.get(i).isClosed())
    //                     Sendtcp(cstr,ssList.get(i));
    //                     else
    //                     {
    //                     ssList.remove(i);   
    //                     //skaddrList.remove(addrid);                          
    //                     }  
    //                     Thread.sleep(20);  
    //                    // addrid ++;                        
    //                 }
    //             }

    //         } catch (Exception EX) {
    //              setText(EX.toString());
    //              //ssList.remove(addrid);     
    //         }
    //         setText("向"+ ssList.size()+ Appdevice.getName() + "设备发送指令" + cstr);


    //         Thread.sleep(60*1000);

    //         addrindex++;
            
    //     }
    // }
}
